package com.example.controller;

import com.alibaba.fastjson2.JSONObject;
import com.example.domain.Result;
import com.example.domain.Store;
import com.example.domain.User;
import com.example.domain.vo.StoreVO;
import com.example.service.StoreService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/store")
public class StoreController extends BaseController {

    @Autowired
    private StoreService storeService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public Result list(Store store) {
        PageInfo<StoreVO> page = this.storeService.selectPageList(store);
        return success(page);
    }

    /**
     * 添加或修改
     */
    @PostMapping("/save")
    public Result save(Store store, HttpSession session) {
        User user = (User) session.getAttribute("login");
        this.storeService.save(store, user);
        return success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Long id) {
        Store store = this.storeService.selectById(id);
        return success(store);
    }

    /**
     * 删除
     */
    @PostMapping("/del/{id}")
    public Result delById(@PathVariable("id") Long id) {
        this.storeService.deleteById(id);
        return success();
    }

    @GetMapping("/options")
    public Result options() {
        List<JSONObject> list = storeService.options();
        return success(list);
    }
}
